package com.shujia.mllib

import org.apache.spark.mllib.linalg
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint

object Demo1Vector {

  def main(args: Array[String]): Unit = {

    /**
      * 稠密向量
      *
      */
    val dense: linalg.Vector = Vectors.dense(Array(0.0, 1.0, 2.0, 4.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0))

    println(dense)

    /**
      * 稀疏向量
      * 只记录有值的位置
      * 节省存储空间
      *
      */

    val sparse: linalg.Vector = Vectors.sparse(12, Array(1, 2, 3, 7), Array(1.0, 2.0, 4.0, 2.0))

    println(sparse)

    //相互转换
    println(sparse.toDense)


    /**
      * LabeledPoint : 一条样本数据
      *
      *
      */

    val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
    println(pos)


    val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)))

    println(neg)
  }

}
